package ge.home.app.Battery;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import ge.home.app.Utils;
import ge.home.app.globals.WebViewGrid;
import ge.home.app.globals.WebViewGridColumn;

/**
 * Created by BACHO on 2/19/14.
 */

public class Battery extends Fragment {
    private class BatteryItemData {
        private String field;
        private String name;
        private String value;

        public BatteryItemData(String field, String name, String value) {
            this.field = field;
            this.name = name;
            this.value = value;
        }

        public String getField() {
            return field;
        }

        public String getName() {
            return name;
        }

        public String getValue() {
            return value;
        }
    }

    private static String name;

    public Battery(String name) {
        this.name = name;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

//        View rootView = inflater.inflate(R.layout.fragment_Battery, container, false);
//        ListView listView = (ListView) rootView.findViewById(R.id.Battery_status_list);
//
        Field[] attributes = BatteryManager.class.getDeclaredFields();
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = getActivity().registerReceiver(null, ifilter);
        WebViewGridColumn[] c = {new WebViewGridColumn("Field", "field"), new WebViewGridColumn("Name", "name"), new WebViewGridColumn("Value", "value")};
        List<BatteryItemData> items = new ArrayList<BatteryItemData>();
        for (Field field : attributes) {
            field.setAccessible(true);
            try {
                Object f = field.get(null);
                if (!(f instanceof String)) continue;
                Object value = batteryStatus.getExtras().get(f.toString());
                String val = value.toString();
                if (field.getName().equals("EXTRA_HEALTH"))
                    val += " (" + checkHealth((Integer) value) + ")";
                if (field.getName().equals("EXTRA_STATUS"))
                    val += " (" + checkCharging((Integer) value) + ")";
                if (field.getName().equals("EXTRA_PLUGGED"))
                    val += " (" + checkPluggedIn((Integer) value) + ")";
                BatteryItemData itemData = new BatteryItemData(field.getName(), f.toString(), val);
                items.add(itemData);
            } catch (Exception e) {
                e.printStackTrace();
                Utils.log("asdasdas dasdasd");
            }
        }
        return new WebViewGrid(getActivity(), c, items);
    }

//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        ((FirstScreen) activity).onSectionAttached(
//                getArguments().getInt(ARG_SECTION_NUMBER));
//    }

    private String checkHealth(int health) {
        switch (health) {
            case BatteryManager.BATTERY_HEALTH_GOOD:
                return "Good";
            case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                return "Over Heat";
            case BatteryManager.BATTERY_HEALTH_DEAD:
                return "Dead";
            case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                return "Over Voltage";
            case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                return "Unspecified Failure";
            default:
                return "Unknown";
        }
    }

    private String checkCharging(int status) {
        switch (status) {
            case BatteryManager.BATTERY_STATUS_CHARGING:
                return "Charging";
            case BatteryManager.BATTERY_STATUS_DISCHARGING:
                return "Dis-charging";
            case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                return "Not charging";
            case BatteryManager.BATTERY_STATUS_FULL:
                return "Full";
            default:
                return "Unknown";
        }
    }

    private String checkPluggedIn(int plugged) {
        switch (plugged) {
            case BatteryManager.BATTERY_PLUGGED_AC:
                return "AC";
            case BatteryManager.BATTERY_PLUGGED_USB:
                return "USB";
            default:
                return "Not Plugged";
        }
    }
}

